#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.component.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Description: 无法补充
 *
 * @author zhang wen yi
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Component
public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;
    // 非@import显式注入，@Component是必须的，且该类必须与main同包或子包
    // 若非同包或子包，则需手动import 注入，有没有@Component都一样
    // 可复制到Test同包测试

    /**
     * Description:
     * <无法补充>
     * @author zhang wen yi
     * @date 15:53 2019/8/8
     * @param context 1
     * @return void
     **/
    @Override
    public void setApplicationContext(ApplicationContext context) {
        synchronized (this) {
            if(applicationContext == null){
                applicationContext = context;
            }
        }
    }

    /**
     * Description:
     * <获取applicationContext>
     * @author zhang wen yi
     * @date 15:54 2019/8/8
     * @return org.springframework.context.ApplicationContext
     **/
    public static synchronized ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * Description:
     * <通过name获取 Bean.>
     * @author zhang wen yi
     * @date 15:54 2019/8/8
     * @param name 1
     * @return java.lang.Object
     **/
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);

    }

    /**
     * Description:
     * <通过class获取Bean>
     * @author zhang wen yi
     * @date 15:54 2019/8/8
     * @param clazz 1
     * @return T
     **/
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * Description:
     * <通过name,以及Clazz返回指定的Bean>
     * @author zhang wen yi
     * @date 15:55 2019/8/8
     * @param name 1
     * @param clazz 2
     * @return T
     **/
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}
